<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/thor-toolkit" prefix="tt" %>
<!DOCTYPE html>
<html>
<head>
	<tt:env/>
    <title>对标管理平台</title>
    <meta charset="utf-8">
    <%@include file="../common-include.jspf"%>
    <script>
         $(function(){
            var list = tui.ctrl.list("systemList");
            
            var groupListData;
            var grouplistFm=tui.ctrl.form("getGroupListForm");
            grouplistFm.on("success",function(data){
            	groupListData=data.jqXHR.responseJSON;
            	list.data(data.jqXHR.responseJSON);
            });
            
            function getUser(id){
            	var fm=tui.ctrl.form();
            	fm.id("getUserForm");
            	fm.action("${root}/action/sysGetAllUser");
            	fm.immediateValue({parent:id}); 
            	fm.submit();
            	fm.once("receive",function(data){
            		if(data.length){
            			tui.ctrl.grid("systemTab").data(data);
            		}else{
            			tui.ctrl.grid("systemTab").data([]);
            		}
            	});
            }
            
            var delGroupId;
           	list.on("rowclick",function(){
           		var id=list.activeItem().key;
           		delGroupId=list.activeItem().key;
           	//	getAllGroup(list.activeItem());
            	getUser(id);
           	});
           	function addGroup(item){
           		var dlg=tui.ctrl.dialog();
            	var groupInput;
            	var groupName;
            	dlg.on("open",function(data){
            		groupInput=tui.ctrl.input("groupInput");
            		groupName=tui.ctrl.input("groupName");
            		$("#hiddenGroup").css("display","block");
            		groupInput.value(item.value);
            		groupInput.readonly(true);
            		
            	});
            	dlg.showResource("editGroupHtml","机构信息",
                    [{name:"保存", func: function(){
                    	var proc = function() {
                            dlg.close();
                            fm.off("success", proc);
                        };
                        var fm=tui.ctrl.form();    
        				fm.id("addGroupForm");
        				fm.action("${root}/action/sysCreateGroup");
        				fm.immediateValue({parent:item.key,name:groupName.value(),permission:item.permission,"sap_id":sap_id});
        				fm.submitForm("getGroupListForm");
        				fm.submit();
                        fm.once("success",proc);
                      }},
                    {name:"取消", func: function(){
                        dlg.close();
                    }}
               	]);
           	}
           	function modifyGroup(item){
           		var dlg=tui.ctrl.dialog();
            	var groupInput;
            	dlg.on("open",function(data){
            		groupInput=tui.ctrl.input("groupName");
            		groupInput.value(item.value);
            	});
            	dlg.showResource("editGroupHtml","机构信息",
                    [{name:"保存", func: function(){
                    	var proc = function() {
                            dlg.close();
                            fm.off("success", proc);
                        };
                        var fm=tui.ctrl.form();    
        				fm.id("modifyGroupForm");
        				fm.action("${root}/action/sysModifyGroupName");
        				fm.immediateValue({id:item.key,name:groupInput.value()});
        				fm.submitForm("getGroupListForm");
        				fm.submit();
                        fm.once("success",proc);
                      }},
                    {name:"取消", func: function(){
                        dlg.close();
                    }}
               	]);
           	}
           	function deleteGroup(id){
        		var fm=tui.ctrl.form();    
				fm.id("delgroupForm");
				fm.action("${root}/action/sysDeleteGroup");
				fm.immediateValue({id:id});
				fm.submitForm("getGroupListForm");
				tui.askbox("确认删除此组及其组中成员?","确认删除",function(result){
            		if(result){
            			fm.submit();
            		}
            	});
        	}
            list.on("rowcontextmenu",function(data){
            	 delGroupId=list.activeItem().key;
            	var item=list.activeItem();
            	var id=item.key;
            	var rowData=list.activeItem();
            	var menu=[];
            	if((rowData.permission&2)===2){
            		menu.push({key:'addGroup',value:'新建用户组'},{key:'addUser',value:'新建成员'});
            	}
            	if((rowData.permission&8)===8){
            		menu.push({key:'modify',value:'修改'});
            	}
            	if((rowData.permission&4)===4){
            		menu.push({key:'del',value:'删除'});
            	}
                
                var m = tui.ctrl.menu(menu);
                m.show({x: data.event.clientX, y: data.event.clientY}, "rT");
                m.on("select",function(data){
                    var checked=data.item.key;
                    if(checked==='addGroup'){
                    	addGroup(item);
                    }
                    if(checked==='modify'){
                    	modifyGroup(item);
                    }
                    if(checked==='addUser'){
                    	setUserInfo(1,id);
                    }
                    if(checked==='del'){
                    	deleteGroup(id);
                    }
                });
               return tui.cancelDefault(data.event);
            });
            
            var grid= tui.ctrl.grid("systemTab");
            grid.columns([
                                 { name: '账号', key: 'account' },
                                 { name: '姓名', key: 'name' },
                                 { name: '电子邮箱', key: 'email' },
                                 { name: '状态', key: 'disabled',format:function(data){
                                     if(data.rowIndex<0){
                                         return;
                                     }
                                     if(data.value===false){
                                          data.cell.firstChild.innerHTML = "<span>可用</span>" ;
                                     }else{
                                          data.cell.firstChild.innerHTML = "<span>禁用</span>" ;
                                     }
                               } }
                           ]);
            
            function modifyPwd(id){
            	var dlg=tui.ctrl.dialog();
            	dlg.on("open",function(){
            		tui.ctrl.input("userId").value(id);
            	});
            	dlg.showResource("modifyUserPwdHtml","修改密码",
                     [
                        {name:"保存", func: function(){
                     	   var proc = function() {
                                dlg.close();
                                fm.off("success", proc);
                            };
                            var fm=tui.ctrl.form("modifyUserPwdForm");
                        	   fm.submit();
                            fm.once("success",proc);
                            fm.once("success",function(){
                         	   tui.infobox("成功修改密码!");
                            });
                        }},
                        {name:"取消", func: function(){
                            dlg.close();
                        }},
                 	]); 
            	
            }
            function deleteUser(item){
				var fm=tui.ctrl.form();    
				fm.id("delAnalysisForm");
				fm.action("${root}/action/sysDeleteUser");
				fm.immediateValue({id:item.id});
				tui.askbox("确认删除?","确认删除",function(result){
            		if(result){
            			fm.submit();
            			getUser(delGroupId);
            		}
            	});
            }
            grid.on("rowcontextmenu",function(data){
            	var menu=[
            	          {key:'edit',value:'编辑'},
            	          {key:'modifyPwd',value:'修改密码'},
            	          {key:'del',value:'删除'}];
            	var m = tui.ctrl.menu(menu);
                m.show({x: data.event.clientX, y: data.event.clientY}, "rT");
                m.on("select",function(data){
                    var checked=data.item.key;
                    var item=grid.activeItem();
                    if(checked==="edit"){
                    	setUserInfo(0,item);
                    }
                    if(checked==="modifyPwd"){
                    	modifyPwd(item.id);
                    }
                    if(checked==="del"){
                    	deleteUser(item,checked);
                    }
                });
               return tui.cancelDefault(data.event);
            });
            
            function setUserInfo(action,item){
            	//action=1为新增,action=0为修改
            	 var dlg=tui.ctrl.dialog();
           		var title;
           		var group;
           		if(action===1){
           			title="新建用户";
           		}
           		if(action===0){
           			title="修改用户";
           		}
           		dlg.on("open",function(data){
           			group=tui.ctrl.input("groupInput");
               		group.data(groupListData);
               		dis=tui.ctrl.input("disabled");
               		if(action===1){
               			if(item){
               				group.readonly(true);
                       		group.value(item);
               			}
               		}
           			if(action===0){
           				$(".hidden-place").css("display","none");
                   		group.readonly(true);
                   		group.value(item.group_id);
                   		
                   		tui.ctrl.input("account").value(item.account);
                   		tui.ctrl.input("company").value(item.company);
                   		tui.ctrl.input("user_name").value(item.name);
                   		tui.ctrl.input("email").value(item.email);
                   		tui.ctrl.input("phone").value(item.phone);
                   		
                   		dis.checked(item.disabled);
           			}
               	});
            		
               	dlg.showResource("createUserHtml",title,
                       [
                          {name:"保存", func: function(){
                       	   var proc = function() {
                                  dlg.close();
                                  fm.off("success", proc);
                              };
                              var fm=tui.ctrl.form("setUserInfoForm");
                              if(action===0){
                           	   fm.action("${root}/action/sysModifyUser");
                           	   tui.ctrl.input("userIdInput").value(item.id);
                           	   tui.ctrl.input("userIdInput").field("id");
                           	   tui.ctrl.input("userIdInput").form("setUserInfoForm");
	                           tui.ctrl.input("password").field("");
	                           tui.ctrl.input("password").removeAttr("data-validator");
	                           tui.ctrl.formAgent("disableAgent").value(dis.checked());
	                    	   fm.submit();
	                           getUser(item.parent);
                              }else
                              if(action===1){
                           	   fm.action("${root}/action/sysCreateUser");
	                           tui.ctrl.formAgent("disableAgent").value(dis.checked());
	                  	   	   fm.submit();
	                  	   	   fm.on("success",function(data){
	                  	   			getUser(group.value());
		                           	if(item){
		                           		getUser(item);
		                           	}
	                  	   	   });
                              }
                              fm.once("success",proc);
                          }},
                          {name:"取消", func: function(){
                              dlg.close();
                          }},
                   	]); 
            }
            tui.ctrl.button("createUserBtn").on("click",function(data){
            	setUserInfo(1);
            });
         });
    </script>
</head>
<script type="text/html" id="createUserHtml">
    <div style="padding:0 20px;width:340px">
        <span class="tui-label lable-white" style="width:85px;margin-left: -12px;">所属用户组：</span><span id="groupInput"  class="tui-input" data-type="select" data-validator= "{'*any':'请选择所属机构'}" data-field="parent" data-form="setUserInfoForm"></span><br>
        <span class="tui-label lable-white">单位部门：</span><span id="company" class="tui-input" data-validator= "{'*any':'请填写单位部门'}" data-field="company" data-form="setUserInfoForm"></span><br>
		<span class="tui-label lable-white">登录账号：</span><span id="account" class="tui-input" data-validator= "{'*any':'请填写登录账号'}" data-field="account" data-form="setUserInfoForm"></span><br>
		<span class="tui-label lable-white hidden-place">密码：</span><span id="password" class="tui-input hidden-place" data-type="password" data-validator= "{'*any':'请填写密码'}" data-field="password" data-form="setUserInfoForm"></span><br class="hidden-place">
        <span class="tui-label lable-white hidden-place">重复密码：</span><span id="repassword" class="tui-input hidden-place " data-type="password" data-validator="{'*same:password':'密码不一致'}" data-form="setUserInfoForm"></span><br class="hidden-place">         
		<span class="tui-label lable-white">姓名：</span><span id="user_name" class="tui-input" data-validator= "{'*any':'请填写姓名'}" data-field="name" data-form="setUserInfoForm"></span><br> 
        <span class="tui-label lable-white ">电子邮箱：</span><span id="email" class="tui-input " data-validator= "{'*any':'请填写电子邮箱'}" data-field="email" data-form="setUserInfoForm"></span><br>
        <span class="tui-label lable-white ">电话：</span><span id="phone" class="tui-input " data-validator= "{'*any':'请填写电话'}" data-field="phone" data-form="setUserInfoForm"></span><br>
		<span class="tui-label lable-white"></span><span class="tui-checkbox" id="disabled" data-form="setUserInfoForm" >禁用账号</span>
		<span class="tui-hidden" id="userIdInput"></span>
</div>
</script>
<script type="text/html" id="editGroupHtml">
    <div style="padding:0 20px;width:340px">
		<div id="hiddenGroup" style="display:none"><span class="tui-label lable-white">所属机构：</span><span id="groupInput" class="tui-input" data-validator= "{'*any':'请填写名称'}" data-field="account" data-form="setUserInfoForm"></span></div>
        <span class="tui-label lable-white">名称：</span><span id="groupName" class="tui-input" data-validator= "{'*any':'请填写名称'}" data-field="account" data-form="setUserInfoForm"></span><br>
	</div>
</script>
<script type="text/html" id="modifyUserPwdHtml">
    <div style="width:400px;text-align:center;padding:20px 0">
        <span class="tui-label lable-white">新密码：</span><span id="newPwd" class="tui-input" data-type="password" data-validator= "{'*any':'请填写新密码'}" data-form="modifyUserPwdForm"></span><br>
        <span class="tui-label lable-white">重复密码：</span><span class="tui-input" data-type="password" data-validator="{'*same:newPwd':'密码不一致'}" data-form="modifyUserPwdForm"></span>
		<span class="tui-input tui-hidden" id="userId"></span>   
	</div>
</script>
</head>

<body style="margin:0px;background-color:#E7E9ED;">
    <%@include file="../common-header.jspf"%>
    <div style="width: 90%; min-width: 1200px;margin:0 auto" >
        <div id="userNOrganizations">
            <div style="float:right;margin:10px 14px 10px 0px">
                <a id="createUserBtn" class="tui-button">新建用户</a>
            </div>
            <div  style="clear:both; text-align:center" >
                <div id="systemList" class="tui-list"  data-key-is-link="true" 
                style="height:550px;background-color:#fafafa;
                box-shadow: inset 1px 1px 3px rgba(100,100,100,0.1);width:23%;display:inline-block"></div>
                <div id="systemTab" class="tui-grid"
                    data-resizable="true"
                  data-rowselectable="true"
                  style="width:73%;display:inline-block;margin-left:10px;height:550px"></div>
            </div>
        </div>
    </div>
</body>
<span id="setUserInfoForm" class="tui-form">
	<span id="disableAgent" data-field="disabled"></span>
</span>
<span id="modifyUserPwdForm" class="tui-form" data-action="${root}/action/sysChangeUserPassword">
	<span data-field="newPassword" data-target="newPwd"></span>
	<span data-field="userId" data-target="userId"></span>
</span>
</html>
